<template>
    <!-- 商品分类导航 -->
    <div class="type-nav">
        <div class="container">
            <!--判断nav的外层区域是否有鼠标移入移除,从而展示和隐藏nav-->
            <div @mouseenter="navIsEnter = true" @mouseleave="navIsEnter = false">
                <h2 class="all">全部商品分类</h2>
                <transition name="nav">
                    <div class="sort" v-show="navIsShow">
                        <div class="all-sort-list2" @mouseenter="isLeave = false" @mouseleave="handleMouseLeave">
                            <div
                                class="item"
                                v-for="(one, index) in categoryList"
                                :key="one.categoryId"
                                @mouseenter="handleMouseEnterThrottle(index)"
                                :class="index === currentIndex ? 'active' : ''"
                                @click="toSearch"
                            >
                                <h3>
                                    <!--声明式导航会出现卡顿现象,因为router-link转换成a标签的时候计算量比较大,所以采用编程式导航-->
                                    <a :data-categoryName="one.categoryName" :data-category1Id="one.categoryId">
                                        {{ one.categoryName }}
                                    </a>
                                </h3>
                                <div class="item-list clearfix">
                                    <div class="subitem">
                                        <dl class="fore" v-for="two in one.categoryChild" :key="two.categoryId">
                                            <dt>
                                                <a
                                                    :data-categoryName="two.categoryName"
                                                    :data-category2Id="two.categoryId"
                                                >
                                                    {{ two.categoryName }}
                                                </a>
                                            </dt>
                                            <dd>
                                                <em v-for="three in two.categoryChild" :key="three.categoryId">
                                                    <a
                                                        :data-categoryName="three.categoryName"
                                                        :data-category3Id="three.categoryId"
                                                    >
                                                        {{ three.categoryName }}
                                                    </a>
                                                </em>
                                            </dd>
                                        </dl>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </transition>
            </div>
            <nav class="nav">
                <a>服装城</a>
                <a>美妆馆</a>
                <a>尚品汇超市</a>
                <a>全球购</a>
                <a>闪购</a>
                <a>团购</a>
                <a>有趣</a>
                <a>秒杀</a>
            </nav>
        </div>
    </div>
</template>

<script>
import { mapState } from "vuex";
import { throttle } from "lodash";

export default {
    name: "TypeNav",
    data() {
        return {
            currentIndex: -1, // 当前选中的一级分类的索引, 默认没有选中
            isLeave: true, // 鼠标是否离开了一级分类, 默认离开了
            navIsEnter: false, // 保存三级分类区域整体区域,是否鼠标移入
        };
    },
    methods: {
        // 鼠标移入一级分类
        handleMouseEnter(index) {
            // 在核心代码触发前做一个判断,是否鼠标已经移出整个区域
            if (this.isLeave) {
                return;
            }
            this.currentIndex = index;
        },
        // 鼠标移出一级分类
        handleMouseLeave() {
            this.currentIndex = -1;
            this.isLeave = true; // 鼠标离开了一级分类
        },

        // 鼠标移入一级分类节流函数,有小bug
        handleMouseEnterThrottle: throttle(
            function (index) {
                this.handleMouseEnter(index);
            },
            300,
            {
                leading: true, // 第一次触发
                trailing: true, // 最后一次触发
            },
        ),

        // 跳转到搜索页面
        toSearch(e) {
            // 获取点击的分类名称和分类id
            const {
                categoryname: categoryName,
                category1id: category1Id,
                category2id: category2Id,
                category3id: category3Id,
            } = e.target.dataset;
            // 根据categoryName是否存在，判断当前点击的是否是a标签
            if (!categoryName) return; // 不是a标签，直接return
            // 跳转到搜索页面
            this.$router.push({
                name: "Search",
                // 传递参数
                query: {
                    categoryName,
                    category1Id,
                    category2Id,
                    category3Id,
                },
                params: this.$route.params, // 合并路由参数
            });
        },
    },
    computed: {
        // 分类列表
        ...mapState("category", ["categoryList"]),

        // 三级分类区域整体区域,是否鼠标移入
        navIsShow() {
            // 判断是否是home页面
            if (this.$route.name === "Home") {
                return true;
            }
            // 判断是否是search页面
            !this.$route.path.includes("search");
            return this.navIsEnter;
        },
    },
    watch: {
        // 监视路由变化
        $route() {
            // 一旦路由发生变化,就将三级分类区域整体区域,是否鼠标移入设置为false
            this.navIsEnter = false;
        },
    },
};
</script>

<style lang="less" scoped>
.type-nav {
    border-bottom: 2px solid #e1251b;

    // 鼠标移入a标签,显示小手
    a {
        cursor: pointer;
    }

    // 动画效果
    .nav-enter,
    .nav-leave-to {
        opacity: 0;
        height: 0;
    }

    .nav-enter-active,
    .nav-leave-active {
        transition: all 0.3s linear;
    }

    .container {
        width: 1200px;
        margin: 0 auto;
        display: flex;
        position: relative;

        .all {
            width: 210px;
            height: 45px;
            background-color: #e1251b;
            line-height: 45px;
            text-align: center;
            color: #fff;
            font-size: 14px;
            font-weight: bold;
        }

        .nav {
            a {
                height: 45px;
                margin: 0 22px;
                line-height: 45px;
                font-size: 16px;
                color: #333;
            }
        }

        .sort {
            position: absolute;
            left: 0;
            top: 45px;
            width: 210px;
            height: 461px;
            position: absolute;
            background: #fafafa;
            z-index: 999;

            .all-sort-list2 {
                .item {
                    h3 {
                        line-height: 30px;
                        font-size: 14px;
                        font-weight: 400;
                        overflow: hidden;
                        padding: 0 20px;
                        margin: 0;

                        a {
                            color: #333;
                        }
                    }

                    .item-list {
                        display: none;
                        position: absolute;
                        width: 734px;
                        min-height: 460px;
                        background: #f7f7f7;
                        left: 210px;
                        border: 1px solid #ddd;
                        top: 0;
                        z-index: 9999 !important;

                        .subitem {
                            float: left;
                            width: 650px;
                            padding: 0 4px 0 8px;

                            dl {
                                border-top: 1px solid #eee;
                                padding: 6px 0;
                                overflow: hidden;
                                zoom: 1;

                                &.fore {
                                    border-top: 0;
                                }

                                dt {
                                    float: left;
                                    width: 54px;
                                    line-height: 22px;
                                    text-align: right;
                                    padding: 3px 6px 0 0;
                                    font-weight: 700;
                                }

                                dd {
                                    float: left;
                                    width: 415px;
                                    padding: 3px 0 0;
                                    overflow: hidden;

                                    em {
                                        float: left;
                                        height: 14px;
                                        line-height: 14px;
                                        padding: 0 8px;
                                        margin-top: 5px;
                                        border-left: 1px solid #ccc;
                                    }
                                }
                            }
                        }
                    }

                    &.active {
                        & > h3 {
                            background: skyblue;
                        }

                        .item-list {
                            display: block;
                        }
                    }
                }
            }
        }
    }
}
</style>
